var member_id = member_name = store_id = pay_name = shuju = address_id = buy_city_id = '';
var ifcart = vat_hash = offpay_hash = offpay_hash_batch = '';
var ws = '';
document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	shuju = ws.shuju;
	pay_name = ws.payment_type;
	ifcart = ws.ifcart;
	store_ids = ws.store_id;
	member_id = plus.storage.getItem('id');
	store_id = plus.storage.getItem('store_id');
	member_name = plus.storage.getItem('name');
//	console.log(member_id+'---'+shuju+'--'+ifcart); 
	goods_inf();

	$('.Good').on('click',function(){                 //提交订单
		 var school_turename = $('.school_turename').val();
		 var school_phone = $('.school_phone').val();
		 var school_add = $('.school_add').val();
		 var remarks = $('.remarks').val();
//		 console.log(shuju+'--'+ifcart+'--'+pay_name+'--'+member_id+'--'+remarks+'--'+vat_hash+'--'+school_turename+'--'+school_phone+'---'+school_add);
		 if(!school_turename){
		 	toast('姓名不能为空！');return;
		 }
		 if(!school_phone){
		 	toast('电话不能为空！');return;
		 }
		 if(!school_phone.match(p1)){
		 	toast('请输入正确的手机号码！');return;
		 }
		 if(!school_add){
		 	toast('地址不能为空！');return;
		 } 
		 $.ajax({  
		 	type:"post",
		 	url:apiRoot + "?act=member_buy&op=buy_step2", 
		 	data:{
		 		cart_id:shuju,
		 		pay_name:pay_name,
		 		member_id:member_id,
		 		pay_message:remarks,
		 		vat_hash:vat_hash,
		 		school_turename:school_turename,
		 		school_phone:school_phone,
		 		school_add:school_add,
		 		ifcart:ifcart,
		 		ordertype:3
		 	},
		 	dataType:'json',
		 	success:function(data){
		 		console.log(JSON.stringify(data));
		 		if(data.state){
		 			var goods_total = $('.goods_total').text();
		 			plus.nativeUI.actionSheet({cancel:"取消",buttons:[{title:"余额支付"},{title:"支付宝支付"},{title:"微信支付"}]},function(e){
						if(e.index == 1){
							$('.password-of-pay-wraper').css("display","block");
							$('#password_backdrop').css("display","block");
							$('.btn').slideUp()				
							
							//如果用户选择的是余额支付，则执行如下代码
							tool.shade(function (){
								$("#j_password_of_pay").hide();
							}).show();  
							//默认让第一个输入框获得焦点
							setTimeout(function (){
								$("#j_password_of_pay").show().find("input:password").eq(0).focus();
							},10); 
							var pwdInputs = $("#j_password_of_pay input:password");
							pwdInputs.each(function (index,item){
								$(this).on("keyup",function (){
									if(!tool.isEmpty($(this).val())){
										$(this).next().focus();
										//如果是最后一个输入框，则执行ajax操作
										if(index == pwdInputs.size() - 1){
											var pwd = "";
											$(this).blur();
											pwdInputs.each(function (){
												pwd += $(this).val();
											});
											yuezhifu(data.order_key_id,pwd); 
			//									alert("用户输入的密码是："+ pwd +"，发送ajax，检查用户输入密码是否正确");	
			//									$("#j_password_of_pay").hide();
			//									tool.shade().hide();
			//									$('.popover').fadeOut();
										}
									}
								});
							});
							
						}else if(e.index == 2){
							ptype = 'alipay';
							payment(data.pay_sn,goods_total);
						}else if(e.index == 3){
							ptype = 'wxpay';
							payment(data.pay_sn,goods_total);
						}else{
							toast("取消支付,订单已生成！");
						ws.close();
						}
					}); 
		 		}else{
		 			toast(data.datas.error);
		 		}
		 	},
		 	error:function(e){
		 		console.log(JSON.stringify(e));
		 	}
		 });
	})
	
},false)

function myaddr(){
	var nwaiting = plus.nativeUI.showWaiting();//显示原生等待框
	var webviewContent= plus.webview.create('my-addr.html','my-addr.html',{},{spage:ws.id});//后台创建webview并打开show.html
	webviewContent.addEventListener("loaded", function() { //注册新webview的载入完成事件
        nwaiting.close(); //新webview的载入完毕后关闭等待框
        webviewContent.show("slide-in-right",500); //把新webview窗体显示出来，显示动画效果为速度200毫秒的右侧移入动画
	}, false);
}

function goods_inf(){
//	console.log(apiRoot + "?act=member_buy&op=buy_step1&member_id="+member_id+"&cart_id="+shuju+"&ifcart="+ifcart+"&flag=1"); 
	plus.nativeUI.showWaiting('加载中...');
	$.ajax({
		type:"post",
		url:apiRoot + "?act=member_buy&op=buy_step1",
		data:{
			member_id:member_id,
			cart_id:shuju,
			ifcart:ifcart,
			flag:1
		},
		dataType:'json',
		success:function(data){
			console.log(JSON.stringify(data));
			if(data){ 
				if(data.store_ids.length > 0){ 
					var html = '';
					var goods_total = goods_freight = 0;
					$.each(data.store_ids, function(k1,v1) {  
						$.each(data.store_cart_list[v1].goods_list, function(k,v) {
							goods_freight = goods_freight + parseFloat(v.goods_freight);
							goods_total = goods_total + (parseFloat(v.goods_price) * parseFloat(v.goods_num));
							$('.mui-media-object').attr('src',pics + v.store_id+'/'+v.goods_image);
							$('.mui-media-body b').text(v.goods_name);
							$('.mui-media-body p').eq(0).text(v.goods_custom[1].value);
							$('.mui-media-body p').eq(1).text(v.goods_custom[2].value);
							$('.school_type').text(v.goods_custom[3].value); 
							$('.price b').text('￥'+v.goods_price);			//价格
							$('.price s').text('市场价：'+v.goods_marketprice);    //市场价
						})	
					});
					$('.goods_total').text((goods_total + goods_freight).toFixed(2));
				}
				vat_hash = data.vat_hash;
				plus.nativeUI.closeWaiting();
			}
		},
		error:function(e){
			plus.nativeUI.closeWaiting();
			error();
		}
	});
}
